
<script setup lang="ts">
import { getGoodsByIdAPI } from '@/services/goods';
import type { GoodsResult } from '@/types/goods';
import { onLoad } from '@dcloudio/uni-app';
import { ref } from 'vue';
import ServicePanel from './components/ServicePanel.vue';
import AddressPanel from './components/AddressPanel.vue';
import { useMemberStore } from '@/stores';

//接收页面参数
const query = defineProps<{
  id:string
}>()

//获取详细信息
const goods = ref<GoodsResult>()
const getGoodsByIdData = async () => {
  const res = await getGoodsByIdAPI(query.id)
  goods.value = res.result
}

onLoad( () => {
  getGoodsByIdData()
})

//轮播
const currentIndex = ref(0)
const onChange: UniHelper.SwiperOnChange = (ev) => {
  currentIndex.value = ev.detail.current
}
//点击图片
const onTapImage = (url: string) => {
  uni.previewImage({
    current: url,
    urls:goods.value!.mainPictures,
  })
}

//点击收货地址
const onTapAddress = () => {
  const memberStore = useMemberStore()
  if (memberStore.profile != undefined) {
    // openPopup('address')
    uni.navigateTo({ url: '/pagesMember/address/address' })
  } else {
    uni.navigateTo({ url: '/pages/login/login' })
  }
}

const popup = ref<{
  open: (type?: UniHelper.UniPopupType) => void
  close: () => void
}>()

const popupName = ref<'address' | 'service'>()
const openPopup = (name: typeof popupName.value) => {
  popupName.value = name
  popup.value?.open()
}

</script>

<template>
  <scroll-view scroll-y class="viewport">
      <view class="preview">
        <swiper
          circular
          class="swiper"
          @change="onChange"
        >
        <swiper-item v-for="item in goods?.mainPictures" :key="item">
            <image
              mode="aspectFill"
              :src="item"
              @tap="onTapImage(item)"
            />
          </swiper-item>
        </swiper>

        <view class="indicator">
          <text>{{ currentIndex + 1 }}/{{ goods?.mainPictures.length }}</text>
        </view>
      </view>

      <view class="priceBg">
        <text class="symbol">¥</text>
        <text>{{ goods?.price }}</text>
      </view>
      <view class="name ellipsis">{{ goods?.name }}</view>
      <view class="desc">{{ goods?.desc  }}</view>

      <view class="action">
        <view class="item arrow">
          <text class="label">选择</text>
          <text class="text ellipsis"> 请选择商品规格 </text>
        </view>
        <view class="item arrow" @tap="onTapAddress()">
          <text class="label">送至</text>
          <text class="text ellipsis"> 请选择收货地址 </text>
        </view>
        <view @tap="openPopup('service')" class="item arrow">
          <text class="label">服务</text>
          <text class="text ellipsis"> 无忧退 快速退款 免费包邮 </text>
        </view>
      </view>

      <view class="detail panel">
        <view class="title">详情</view>
        <view class="content">
          <view class="properties">
            <view class="item" v-for="item in goods?.details.properties" :key="item.name">
              <text class="label"> {{ item.name }} </text>
              <text class="value"> {{ item.value }}</text>
            </view>
          </view>

          <image
            v-for="item in goods?.details.pictures"
            :key="item"
            :src="item"
            mode="widthFix"
          />
        </view>
      </view>

      <view class="similar panel">
        <view class="title">同类推荐</view>
        <view class="content">
          <navigator
            :url="`/pages/goods/goods?id=${item.id}`"
            hover-class="none"
            v-for="item in goods?.similarProducts"
            :key="item.id"
            class="goods"
          >
            <image
              :src="item.picture"
              mode="aspectFill"
              class="image"
            />
            <view class="name ellipsis">{{ item.name }}</view>
            <view class="price">
              <text class="symbol">¥</text>
              <text class="number">{{ item.price }}</text>
            </view>
          </navigator>
        </view>
      </view>

  </scroll-view>

  <view class="toolbar">
    <view class="icons">
      <button class="icons-button">
        <text class="icon-heart"></text>
        收藏
      </button>
      <button class="icons-button">
        <text class="icon-handset"></text>
        客服
      </button>
      <button class="icons-button">
        <text class="icon-cart"></text>
        购物车
      </button>
    </view>
    <view class="buttons">
      <view class="addcart">加入购物车</view>
      <view class="buynow">立即购买</view>
    </view>
  </view>

  <uni-popup
    ref="popup"
    type="bottom"
    background-color="#fff"
  >
    <ServicePanel v-if="popupName === 'service'" @close="popup?.close()"/>
    <AddressPanel v-if="popupName === 'address'" @close="popup?.close()"/>
  </uni-popup>
</template>

<style lang="scss">
  
Page {
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.viewport {
  height: 100%;
}

.preview {
  width: 100%;
  height: 700rpx;
  position: relative;

  .swiper {
    height: 100%;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .indicator {
    position: absolute;
    right: 20rpx;
    bottom: 20rpx;
    background-color: rgba(0, 0, 0, 0.3);
    padding: 2rpx 20rpx;
    border-radius: 20rpx;
    color: white;
    font-size: 22rpx;
  }
}

.priceBg {
  background-color: #27BA9B;
  width: 100%;
  height: 100rpx;
  color: white;
  font-size: 40rpx;
  line-height: 100rpx;
  padding-left: 20rpx;

  .symbol {
    font-size: 24rpx;
  }
}

.name {
  margin: 20rpx;
  max-height: 80rpx;
  overflow: hidden;
  text-overflow: ellipsis;
}

.desc {
  margin: 20rpx;
  font-size: 26rpx;
  color: #cf4444;
}

.action {
  border-top: 1rpx solid lightgray;
  border-bottom: 1rpx solid lightgray;

  .item {
    margin: 0 20rpx;
    min-height: 80rpx;
    padding-right: 60rpx;
    border-bottom: 1rpx solid lightgray;
    display: flex;
    align-items: center;
    &:last-child {
      border-bottom: 0 none;
    }

    .label {
      color: gray;
      width: 80rpx;
    }

    .text {
      margin: 10rpx 0 10rpx 0;
      flex: 1;
      // -webkit-line-clamp: 1;
    }
  }
}

.arrow {
  &::after {
    top: 50%;
    right: 30rpx;
    color: #ccc;
    content: '\e6c2';
    font-size: 32rpx;
    transform: translateY(-50%);
    position: absolute;
  }
}

.detail {
  padding-left: 20rpx;
  .content {
    margin-left: -20rpx;
    image {
      width: 100%;
    }
  }
  .properties {
    padding: 0 20rpx;
    margin-bottom: 30rpx;
    .item {
      display: flex;
      line-height: 2;
      padding: 10rpx;
      font-size: 26rpx;
      color: #333;
      border-bottom: 1rpx dashed #ccc;
    }
    .label {
      width: 200rpx;
    }
    .value {
      flex: 1;
    }
  }
}

.similar {
  .content {
    padding: 0 20rpx 200rpx;
    background-color: #f4f4f4;
    display: flex;
    flex-wrap: wrap;

    .goods {
      width: calc((100% - 28rpx) / 2);
      min-width: calc((100% - 28rpx) / 2);
      max-width: calc((100% - 28rpx) / 2);
      padding: 20rpx 20rpx;
      margin: 20rpx 7rpx;
    }

    .image {
      width: 100%;
      height: 260rpx;
    }

    .name {
      height: 80rpx;
      margin: 10rpx 0;
      font-size: 26rpx;
      color: #262626;
    }

    .price {
      line-height: 1;
      font-size: 20rpx;
      color: #cf4444;
    }

    .number {
      font-size: 26rpx;
      margin-left: 2rpx;
    }
  }

  navigator {
    &:nth-child(even) {
      margin-right: 0;
    }
  }
}

.toolbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  border-top: 1rpx solid #eaeaea;
  justify-content: space-between;
  align-items: center;
  box-sizing: content-box;
  z-index: 1;
  background-color: white;
  padding: 20rpx 20rpx 20rpx;
  .icons {
    padding-right: 10rpx;
    display: flex;
    align-items: center;
    flex: 1;
    .icons-button {
      flex: 1;
      font-size: 20rpx;
      color: #333;
      background-color: white;
      border-radius: 0;
      text-align: center;
      line-height: 1.4;
      padding: 0;
      margin: 0;
      &::after {
        border: none;
      }
    }

    text {
      display: block;
      font-size: 34rpx;
    }
  }

  .buttons {
    display: flex;

    view {
      width: 220rpx;
      text-align: center;
      color: white;
      font-size: 26rpx;
      border-radius: 72rpx;
      line-height: 72rpx;
    }
    .addcart {
      background-color: #ffa868;
      margin-right: 20rpx;
    }
    .buynow {
      background-color: #27ba9b;
    }
  }
}

</style>